﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Autofac;
using System.Reflection;

namespace Simple.Common.Infrastructure.DependencyManagement
{
    public class BaseDependencyRegistrar : IDependencyRegistrar
    {
        public void Register(ContainerBuilder builder, ITypeFinder typeFinder)
        {
            Assembly[] assemblys = typeFinder.GetAssemblies().ToArray();
            Type typeITransientDependency = typeof(ITransientDependency);
            Type typeISingletonDependency = typeof(ISingletonDependency);

            builder.RegisterAssemblyTypes(assemblys).Where(m => typeITransientDependency.IsAssignableFrom(m)).AsSelf().InstancePerDependency();
            builder.RegisterAssemblyTypes(assemblys).Where(m => typeISingletonDependency.IsAssignableFrom(m)).AsSelf().AsImplementedInterfaces().SingleInstance();
        }

        /// <summary>
        /// 最后执行
        /// </summary>
        public int Order
        {
            get { return 9999; }
        }
    }
}
